defmodule ProgContest.QuizzesTest do
  use ProgContest.DataCase
  use ProgContest.Fixtures

  alias ProgContest.Quizzes
  alias ProgContest.Contests

  setup do
    [contest: contest_fixture()]
  end

  describe "quizzes" do
    alias ProgContest.Quizzes.Quiz

    test "list_quizzes/0 returns all quizzes with the given contest id", %{contest: contest} do
      quiz = quiz_fixture(contest)
      assert Quizzes.list_quizzes(contest.id) == [quiz]
    end

    test "list_quizzes/0 does not return quizzes that belong to other contest", %{contest: contest} do
      quiz_fixture(contest)
      assert Quizzes.list_quizzes(9999) == []
    end

    test "get_quiz!/1 returns the quiz with given id", %{contest: contest} do
      quiz = quiz_fixture(contest)
      assert Quizzes.get_quiz!(quiz.id) == quiz
    end

    test "create_quiz/1 with valid data creates a quiz", %{contest: contest} do
      attrs = Map.put(@valid_quiz_attrs, :contest_id, contest.id)
      assert {:ok, %Quiz{} = quiz} = Quizzes.create_quiz(attrs)
      assert quiz.author == "some author"
      assert quiz.question == "some question"
      assert quiz.title == "some title"
    end

    test "create_quiz/1 with invalid data returns error changeset" do
      assert {:error, %Ecto.Changeset{}} = Quizzes.create_quiz(@invalid_quiz_attrs)
    end

    test "update_quiz/2 with valid data updates the quiz", %{contest: contest} do
      quiz = quiz_fixture(contest)
      assert {:ok, %Quiz{} = quiz} = Quizzes.update_quiz(quiz, @update_quiz_attrs)
      assert quiz.author == "some updated author"
      assert quiz.question == "some updated question"
      assert quiz.title == "some updated title"
    end

    test "update_quiz/2 with invalid data returns error changeset", %{contest: contest} do
      quiz = quiz_fixture(contest)
      assert {:error, %Ecto.Changeset{}} = Quizzes.update_quiz(quiz, @invalid_quiz_attrs)
      assert quiz == Quizzes.get_quiz!(quiz.id)
    end

    test "delete_quiz/1 deletes the quiz", %{contest: contest} do
      quiz = quiz_fixture(contest)
      assert {:ok, %Quiz{}} = Quizzes.delete_quiz(quiz)
      assert_raise Ecto.NoResultsError, fn -> Quizzes.get_quiz!(quiz.id) end
    end

    test "change_quiz/1 returns a quiz changeset", %{contest: contest} do
      quiz = quiz_fixture(contest)
      assert %Ecto.Changeset{} = Quizzes.change_quiz(quiz)
    end
  end
end
